package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.common.zm.cInterface.ce.Note;
import com.baosight.sczx.common.zm.cInterface.ce.WorkPlan;
import com.baosight.sczx.zm.ce.domain.ZMCEEquipmentOrder;
import com.baosight.sczx.zm.ce.domain.ZMCEUnusualProblem;
import com.baosight.sczx.zm.ce.domain.ZMCEWorkPlan;
import com.baosight.sczx.zm.ce.domain.ZMCEWorkRecord;

/**
 * <p>Title: ServiceZMCE20.java</p>
 * <p>Description: 工单反馈</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-13 下午05:32:12  张小飞  Create
 *</pre>
 */
public class ServiceZMCE20 extends ServiceEPBase {

	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-22上午10:48:28 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-22上午10:48:28 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdStatusIn", "'执行','暂停','完成'", true);
		StaticTools.setQueryParameters(inInfo, "fdExecutionDepartmentId", UserSession.getInSessionProperty("orgCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCEEquipmentOrder.query", new ZMCEEquipmentOrder());
		
		outInfo.set("fdDepartmentId", UserSession.getInSessionProperty("orgCode"));
		outInfo.set("fdPersonId", UserSession.getUserId());
		outInfo.set("fdPerson", UserSession.getInSessionProperty("UserCname"));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", false));		
		//outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.ce.people", "empCode", "empName", false));
		
		return outInfo;
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: 反馈</p>
	 * <p>return value: ServiceZMCE20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-22上午10:48:28 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		String fdType = inInfo.getBlock("result").getCellStr(0, "fdType");
		String fdStatus = inInfo.getBlock("result").getCellStr(0, "fdStatus");
		String fdPlanId = inInfo.getBlock("result").getCellStr(0, "fdPlanId");//工作计划主键
		String more = inInfo.getBlock("result").getCellStr(0, "more");
		String equipmentStatus = inInfo.getBlock("result").getCellStr(0, "equipmentStatus");
		
		//修改该工单的状态
		EiInfo outInfo = super.update(inInfo, "ZMCEEquipmentOrder.feedback");
		
		//修改该条计划的状态为完成
		if("完成".equals(fdStatus)){
			//回填计划或者异常的状态
			Note.completeNote(inInfo.getBlock("result").getRow(0));
			
			if("大修".equals(fdType)||"维修".equals(fdType))
			{
				if("1".equals(equipmentStatus))
				{
					if(Equipment.updateEquipmentStatus("在用", "完好", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"))<=0)
					{
						inInfo.setStatus(-1);
						return inInfo;
					}
				}
			}
		}
			
		
		//新增一条工作记录
		EiInfo workInfo=new EiInfo();
		workInfo.addBlock("result").addBlockMeta(new ZMCEWorkRecord().eiMetadata);		
		String recordKey = TablePk.getInstance().getNextValue("T_CE_WORKRECORD");
		workInfo.getBlock("result").setCell(0, "fdKeyId", recordKey);
		workInfo.getBlock("result").setCell(0, "fdIsOrder", "是");
		workInfo.getBlock("result").setCell(0, "fdOrderId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		workInfo.getBlock("result").setCell(0, "fdEquipmentId", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"));
		workInfo.getBlock("result").setCell(0, "fdUseDepartmentId", inInfo.getBlock("result").getCellStr(0, "fdDepartmentId"));
		workInfo.getBlock("result").setCell(0, "fdUseDepartment", inInfo.getBlock("result").getCellStr(0, "fdDepartment"));
		workInfo.getBlock("result").setCell(0, "fdType", inInfo.getBlock("result").getCellStr(0, "fdType"));
		workInfo.getBlock("result").setCell(0, "fdContent", inInfo.getBlock("result").getCellStr(0, "fdWorkContent"));
		workInfo.getBlock("result").setCell(0, "fdDescription", inInfo.getBlock("result").getCellStr(0, "fdDescription"));
		workInfo.getBlock("result").setCell(0, "fdBeganDatetime", inInfo.getBlock("result").getCellStr(0, "fdTrueBeganTime"));
		workInfo.getBlock("result").setCell(0, "fdEndDatetime", inInfo.getBlock("result").getCellStr(0, "fdTrueEndTime"));
		workInfo.getBlock("result").setCell(0, "fdDepartmentId", inInfo.getBlock("result").getCellStr(0, "fdExecutionDepartmentId"));
		workInfo.getBlock("result").setCell(0, "fdPersonId", inInfo.getBlock("result").getCellStr(0, "fdExecutionPerson"));
		super.insert(workInfo, "ZMCEWorkRecord.insert");
		
		//同时保存异常情况进度设备异常记录
		if("1".equals(more)){//勾选汇报异常设备
			workInfo=new EiInfo();
			workInfo.addBlock("result").addBlockMeta(new ZMCEUnusualProblem().eiMetadata);	
			workInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_UNUSUALPROBLEM"));
			
			if("润滑".equals(fdType)){
				workInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备润滑");
			}else if("保养".equals(fdType)){
				workInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备保养");
			}else if("点检".equals(fdType)){
				workInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备点检");
			}else if("大修".equals(fdType)){
				workInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备大修");
			}else if("维修".equals(fdType)){
				workInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备维修");
			}

			workInfo.getBlock("result").setCell(0, "fdIsOrder", "是");
			workInfo.getBlock("result").setCell(0, "fdWorkrecordId", recordKey);
			workInfo.getBlock("result").setCell(0, "fdEquipmentId", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"));
			workInfo.getBlock("result").setCell(0, "fdUseDepartmentId", inInfo.getBlock("result").getCellStr(0, "fdDepartmentId"));
			workInfo.getBlock("result").setCell(0, "fdUseDepartment", inInfo.getBlock("result").getCellStr(0, "fdDepartment"));
			workInfo.getBlock("result").setCell(0, "fdDegree", inInfo.getBlock("result").getCellStr(0, "fdDegree"));
			workInfo.getBlock("result").setCell(0, "fdDescription", inInfo.getBlock("result").getCellStr(0, "fdExceptionDescription"));
			workInfo.getBlock("result").setCell(0, "fdStatus", "未处理");
			workInfo.getBlock("result").setCell(0, "fdPerson", UserSession.getInSessionProperty("UserCname"));
			workInfo.getBlock("result").setCell(0, "fdDatetime", StaticTools.getDateTime());
			
			super.insert(workInfo, "ZMCEUnusualProblem.insert");
		}
		
		return outInfo;
	}
	
}
